package cases;

import org.testng.annotations.Test;

import commonFunction.Log;
import commonFunction.ReadExcelData;
import commonFunction.ScreenShotOnFailure;
import commonFunction.TestngListener;
import pageObject.IndexPage;
import pageObject.LoginPage;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;

import java.net.URL;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;

@Listeners({TestngListener.class})
public class Login2 extends ScreenShotOnFailure {
	/**
	 * 后台登录测试
	 */

//	WebDriver driver= new FirefoxDriver();

	WebDriver driver;
	LoginPage loginPage;
	IndexPage indexPage;	
		
//	Logger logger=Logger.getLogger(Login.class);
	Log logger=new Log(Login2.class);
		
	// 当dataProvider和使用dataProvider方法不在同一个类中时，需用dataProviderClass指明
	@Test(dataProvider = "loginData", dataProviderClass = ReadExcelData.class)
	public void login(String name, String passwd) throws InterruptedException {
		logger.info("===开始登陆：");
		loginPage.login(name, passwd);
		Assert.assertEquals(driver.getTitle(), "优航通-管理系统");
		
		logger.info("===注销：");
		indexPage.logout();
	}


	@BeforeClass
	public void beforeClass() throws Exception {
		
		DesiredCapabilities capability =DesiredCapabilities.firefox();
		capability.setBrowserName("firefox");
//		capability.setBrowserName(browserName);
		
		driver= new RemoteWebDriver(new URL("http://192.168.3.7:4444/wd/hub"), capability);
		
		super.setDriver(driver);// 关键
//		PropertyConfigurator.configure("log4j.properties");
		
		logger.info("===打开网页：");
		driver.get("http://192.168.3.10:8106/ysth-aviation-back/login");
		
		loginPage = PageFactory.initElements(driver, LoginPage.class);
		indexPage = PageFactory.initElements(driver, IndexPage.class);
	}

	@AfterClass
	public void afterClass() {
		logger.info("===退出网页：");
		driver.quit();
	}

}
